package com.controller;

import com.entity.Course;
import com.entity.CourseType;
import com.github.pagehelper.PageInfo;
import com.service.CourseService;
import com.service.CourseTypeService;
import com.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ct")
public class UserCourseTypeController {
    //====张
    @Autowired
    CourseTypeService courseTypeService;
    @Autowired
    CourseService courseService;

    @RequestMapping("/findCourseType.do")
    @ResponseBody
    public Map findCourseType() {
        System.out.println("--------");
        /**
         *  返回父类的类型及id,以及子类的集合Set<CourseType> childrens,先查parent_id是空的id1,再查parent_id是id1的id2,
         *  并把查出来的集合放到id1的查询出来的对象的childrens中
         *  再查parent_id是id2的id3并把查出来的集合放到id2的查询出来的对象的childrens中
         */
            List<CourseType> courseTypes = courseTypeService.findCourseTypes();

            List<Course> courses = courseService.findTop4CourseByGroup();//寻找当前类别最火的四个course
            Map map = new HashMap();
            map.put("courseTypes", courseTypes);
            map.put("courses", courses);
            return map;
    }

    @RequestMapping("/findByParentId.do")
    @ResponseBody
    public List<List<CourseType>> findByParentId(int parent_id) {
        List<List<CourseType>> list = new ArrayList<>();
        List<CourseType> types = courseTypeService.findByParentId(parent_id);
        for (CourseType type : types) {
            list.add(courseTypeService.findByParentId(type.getId()));
        }
        return list;
    }

    //罗  父类分页
//罗  父类分页
//    罗     findBackCourseType
    @RequestMapping("/findBackCourseType.do")
    @ResponseBody
    public PageInfo<CourseType> findBackCourseType(HttpServletRequest request, HttpSession session) {
        PageInfo<CourseType> info = new PageInfo<CourseType>();
        String pageNo = request.getParameter("pageNo");
        String parent_id1 = request.getParameter("parent_id");
        Integer page = null;
        Integer parent_id = null;
        try {

            if (pageNo != null) {
                page = Integer.valueOf(pageNo);
                parent_id = Integer.valueOf(parent_id1);
                System.out.println("-----=========" + parent_id);
                info = courseTypeService.findBackCourseType(page, parent_id);
                session.setAttribute("courseTypeList", courseTypeService.findCourseTypesAll());
                System.out.println("----------" + info.getList());
                return info;
            } else {
                parent_id = Integer.valueOf(parent_id1);
                PageInfo<CourseType> backCourseType1 = courseTypeService.findBackCourseType1(parent_id);
                return backCourseType1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }
    @RequestMapping("/addBackType.do")
    @ResponseBody
    public AjaxResult addBackType(HttpServletRequest request) {
        AjaxResult ajaxResult=new AjaxResult();
//        ajaxResult.setMsg("添加课程分类失败");
        CourseType courseType=new CourseType();
        courseType.setParent_id(Integer.valueOf(request.getParameter("parent_id")));
        courseType.setId(Integer.valueOf(request.getParameter("parent_id")));
        courseType.setType_name(request.getParameter("type_name"));
        courseType.setStatus(Integer.valueOf(request.getParameter("status")));
        courseTypeService.addBackType(courseType);
        ajaxResult.setMsg("添加课程分类成功");
        return ajaxResult;
    }
    @RequestMapping("/toggleStatus.do")
    @ResponseBody
    public Integer toggleStatus(CourseType courseType){
        System.out.println(courseType);
        courseTypeService.toggleStatus(courseType);
        return 0;
    }
    @RequestMapping("/showBackName.do")
    @ResponseBody
    public AjaxResult showBackName(HttpServletRequest request){
        Integer id=Integer.valueOf(request.getParameter("id"));
        return courseTypeService.showBackName(id);
    }
    @RequestMapping("/modifyCourseTypeName.do")
    @ResponseBody
    public Integer modifyCourseTypeName(CourseType courseType){
        courseTypeService.modifyCourseTypeName(courseType);
        return 0;
    }
}
